package com.sxm.yiwei.activitis;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.tools.CastTime;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class BookAddPriceActivity extends BaseActivity implements View.OnClickListener{

    private ImageView addpriceback;
    private TextView bookexplain;
    private TextView bookNo;
    private TextView bookPrice;
    private TextView book_reciver;
    private TextView booksendtime;
    private TextView booktype;
    private EditText bookinaddprice;
    private Button bookup;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private static final int ACTIVITYTYPE = 101;
    private String id1,intro,publishTime,orderCode,type,totalAmount;
    private String from;
    private ImageView backmain;
    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_add_price);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid","1");
        userId = sharedPreferences.getString("userId", "0");
        token = sharedPreferences.getString("token", "2");
        /**
         * listItent.putExtra("intro",intro);
         listItent.putExtra("publishTime",publishTime);
         listItent.putExtra("orderCode",orderCode);
         listItent.putExtra("totalAmount",totalAmount);
         listItent.putExtra("type",type);
         */
        Intent intent = getIntent();
        id1 = intent.getStringExtra("id");
        intro = intent.getStringExtra("intro");
        publishTime = intent.getStringExtra("publishTime");
        orderCode = intent.getStringExtra("orderCode");
        totalAmount = intent.getStringExtra("totalAmount");
        type = intent.getStringExtra("type");
        from = intent.getStringExtra("from");

        initBookVIew();
        if (from.equals("11")){
            getBookContent(id1);
        }else {
            if (type.equals("1")){
                booktype.setText("一键发单");
            }else if (type.equals("2")){
                booktype.setText("急件要发");
            }else {
                booktype.setText("跑腿劳务");
            }
            bookNo.setText(orderCode);
            bookPrice.setText(totalAmount);
            bookexplain.setText(intro);
            String s = CastTime.CastTime(Long.valueOf(publishTime));
            booksendtime.setText(s);
        }

    }

    // 获取订单信息
    private void getBookContent(String id) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.BOOKCONTENT);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        params.addParameter("params", j.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");

                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                String publishTime = obj1.getString("publishTime");
                String orderCode = obj1.getString("orderCode");
                String type = obj1.getString("type");
                String intro = obj1.getString("intro");
                String totalAmount = obj1.getString("totalAmount");

                if (type.equals("1")){
                    booktype.setText("一键发单");
                }else if (type.equals("2")){
                    booktype.setText("急件要发");
                }else {
                    booktype.setText("跑腿劳务");
                }
                bookNo.setText(orderCode);
                bookPrice.setText(totalAmount);
                bookexplain.setText(intro);
                String s = CastTime.CastTime(Long.valueOf(publishTime));
                booksendtime.setText(s);

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(BookAddPriceActivity.this,ex);
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }
        // 请求 获取id
    private void getPickUpId() {
        final String price = String.valueOf(bookinaddprice.getText());
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.BOOK_ADDPRICE);
        params.setHeader("Content-Type","application/json");
        params.setHeader("token", token);
        params.setHeader("appId", appId);
        params.setHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId",id1);
        j.put("amount",price);
        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");

                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                String makeupId = obj1.getString("makeupId");

                // 提交加价数据给支付页面
                Intent i = new Intent(BookAddPriceActivity.this,BookPayActivity.class);
                i.putExtra("from","add");
                i.putExtra("orderId",id1);
                i.putExtra("id",makeupId);
                i.putExtra("orderCode",orderCode);
                i.putExtra("amount",price);
                i.putExtra("alipay",InternetAddressUtis.BOOK_ADDPRICEAILIPAY);
                i.putExtra("yivipay",InternetAddressUtis.BOOK_ADDPRICEPAY);
                startActivityForResult(i,ACTIVITYTYPE);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 你ihao---"+ex);
                ErrType.errType(BookAddPriceActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    //初始化控件
    private void initBookVIew() {
        addpriceback = (ImageView) findViewById(R.id.bookaddprice_back);
        bookexplain = (TextView) findViewById(R.id.book_explain);
        bookNo = (TextView) findViewById(R.id.book_no);
        bookPrice = (TextView) findViewById(R.id.book_price);
        book_reciver = (TextView) findViewById(R.id.book_reciver);
        booksendtime = (TextView) findViewById(R.id.book_sendtime);
        booktype = (TextView) findViewById(R.id.book_type);
        bookinaddprice = (EditText) findViewById(R.id.book_inaddprice);
        bookup = (Button) findViewById(R.id.book_upbt);
        backmain = (ImageView) findViewById(R.id.addprice_backmain);

        addpriceback.setOnClickListener(this);
        bookup.setOnClickListener(this);
        backmain.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bookaddprice_back:
                finish();
                break;
            case R.id.book_upbt:
                // 此处获取加价id
                getPickUpId();

                break;
            case R.id.billselect_backmain:
                startActivity(new Intent(BookAddPriceActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==101){
            builder =  new AlertDialog.Builder(BookAddPriceActivity.this);
            View view = LayoutInflater.from(BookAddPriceActivity.this).inflate(R.layout.amount_dialog, null);
            builder.setView(view);
            Button cancel = (Button) view.findViewById(R.id.amount_cancel);
            Button sure = (Button) view.findViewById(R.id.amount_sure);
            TextView message = (TextView) view.findViewById(R.id.amount_message);
            TextView title = (TextView) view.findViewById(R.id.amount_title);
            cancel.setVisibility(View.GONE);
            title.setText("加价提示");
            message.setText("订单加价成功");
            sure.setText("确定");
            final AlertDialog dialog = builder.create();
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   dialog.dismiss();
                }
            });
            dialog.show();
        }
    }
}
